var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false })

var query_pig = require('./query_pig.js');
var invoke_pig = require('./invoke_pig.js');
query_pig = new query_pig();
invoke_pig = new invoke_pig();

app.get('/', function (req, res) {
    res.redirect('index.html');
});

app.get('/index.html', function (req, res) {
    res.sendFile( __dirname + "/" + "index.html" );
});

app.get('/register_pig.html', function (req, res) {
    res.sendFile( __dirname + "/" + "register_pig.html" );
});

app.get('/quarantine_pig.html', function (req, res) {
    res.sendFile( __dirname + "/" + "quarantine_pig.html" );
});

app.get('/transfer_pig_to_slaughter_house.html', function (req, res) {
    res.sendFile( __dirname + "/" + "transfer_pig_to_slaughter_house.html" );
});

app.get('/query_pig_history.html', function (req, res) {
    res.sendFile( __dirname + "/" + "query_pig_history.html" );
});

app.get('/query_owner_all_pigs.html', function (req, res) {
    res.sendFile( __dirname + "/" + "query_owner_all_pigs.html" );
});

app.get('/delete_pig.html', function (req, res) {
    res.sendFile( __dirname + "/" + "delete_pig.html" );
});

app.post('/process_register_pig', urlencodedParser, function(req, res) {
    console.log("Hit process_register_pig!");
    // peer chaincode invoke -C myc -n mycc -c '{"Args":["initPig","pig3","blue","70","tom"]}'
    var options = {
        chaincode_function: "initPig",
        chaincode_args: [
            req.body.name,
            req.body.color,
            req.body.weight,
            req.body.owner
        ],
    };
    invoke_pig.invoke_chaincode(options, function (resp) {
        res.json(resp);
    });
});

app.post('/process_quarantine_pig', urlencodedParser, function(req, res) {
    console.log("Hit process_quarantine_pig!");
    // peer chaincode invoke -C myc -n mycc -c '{"Args":["quarantinePig","pig1", "true"]}'
    var options = {
        chaincode_function: "quarantinePig",
        chaincode_args: [
            req.body.name,
            req.body.is_pass
        ],
    };
    invoke_pig.invoke_chaincode(options, function (resp) {
        res.json(resp);
    });
});

app.post('/process_transfer_pig_to_slaughter_house', urlencodedParser, function(req, res) {
    console.log("Hit process_transfer_pig_to_slaughter_house!");
    // peer chaincode invoke -C myc -n mycc -c '{"Args":["sendPigToSlaughterHouse","pig1"]}'
    var options = {
        chaincode_function: "sendPigToSlaughterHouse",
        chaincode_args: [
            req.body.name        
        ],
    };
    invoke_pig.invoke_chaincode(options, function (resp) {
        res.json(resp);
    });
});

app.post('/process_query_pig_history', urlencodedParser, function(req, res) {
    console.log("Hit process_query_pig_history!");
    // peer chaincode query -C myc -n mycc -c '{"Args":["getHistoryForPig","pig1"]}'
    var options = {
        chaincode_function: "getHistoryForPig",
        chaincode_args: [
            req.body.name
        ],
    };
    query_pig.query_chaincode(options, function (resp) {
        res.json(resp);
    });
});

app.post('/process_query_owner_all_pigs', urlencodedParser, function(req, res) {
    console.log("Hit process_query_owner_all_pigs!");
    // peer chaincode query -C myc -n mycc -c '{"Args":["queryPigsByOwner","tom"]}'
    var options = {
        chaincode_function: "queryPigsByOwner",
        chaincode_args: [
            req.body.owner
        ],
    };
    query_pig.query_chaincode(options, function (resp) {
        res.json(resp);
    });
});

app.post('/delete_pig', urlencodedParser, function(req, res) {
    console.log("Hit delete_pig!");
    // peer chaincode invoke -C myc -n mycc -c '{"Args":["delete","pig1"]}'
    var options = {
        chaincode_function: "delete",
        chaincode_args: [
            req.body.name
        ],
    };
    invoke_pig.invoke_chaincode(options, function (resp) {
        res.json(resp);
    });
});

var server = app.listen(8081, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log("Server started...");
    console.log("Server with address %s:%s", host, port);
});